<template>
	<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
	  <el-row>
	  	<el-col :span='4' ref='rolename'>角色名称</el-col>
	  	<el-col :span="20"><el-input v-model='role.roleName' /></el-col>
	  </el-row>
	  
	 <el-row>
	  	<el-tree
		  :props="defaultProps"
		  :data="allPermissions"
		  ref='tree'
		  show-checkbox
			node-key="permissionDesc"
		>
		</el-tree>
	  </el-row>
	  
	  <span slot="footer" class="dialog-footer">
	    <el-button @click="handleClose">取 消</el-button>
	    <el-button type="primary" @click="updateRolePermissions()">确 定</el-button>
	  </span>
	</el-dialog>
</template>

<script>
	export default{
		props:['dialogVisible','role'],
		data() {
	      return {
	        defaultProps:{
	        	children:'children',
	        	label:'label',
	        },
	        refs:{}
	      }
	    },
	    computed:{
	    	allPermissions(){
	    		return this.$store.getters.allPermissions;
	    	}
	    },
	    methods: {
	    	handleClose(){
	    		this.$parent.dialogVisible = false
	    	},
	    	//更新角色信息
	    	updateRolePermissions(){
				var permissions = [];
				this.$refs.tree.getCheckedNodes().forEach((items)=>{
					permissions.push(items._id);
				})
				var params = {
					permissions:permissions,
					roleName:this.role.roleName,
					id:this.role._id
				}
				this.$http.post('./updateRoleInfo',params)
				.then((res)=>{
					this.$message({
						message:res.data.success?res.data.message+"成功":res.data.message+"失败",
						type:res.data.success?"success":"danger"	
					})
					
				});
				this.$parent.dialogVisible = false
	    	}
	   },
	   watch: {
	        role(newValue){
	            setTimeout(()=>{
	                //根据要编辑的角色中已有的权限,设置默认选中权限
	                var defaultPermission = [];
	                this.$store.state.allPermissions.forEach((items)=>{
	                    if(newValue.permissions.indexOf(items._id)!=-1){
	                        defaultPermission.push(items);
	                    }
	                })
	                this.$refs.tree.setCheckedNodes(defaultPermission)
	            },0)
	        }
	    }
	}
</script>

<!--<script>
export default {
    props: ['role','dialogVisible'],
    data(){
        return {
            defaultProps: {
                children: 'children',
                label: 'label',
            },
            refs: {}
        }
    },
    computed: {
        allPermissions(){
            return this.$store.getters.allPermissions;   
        }
    },
    methods: {
        handleClose(){
            this.$parent.dialogVisible = false
        },
        //更新角色信息
        updateRolePermissions(){
            var permissions = [];
            this.$refs.tree.getCheckedNodes().forEach((item)=>{
                permissions.push(item._id);
            })
            var params = {
                permissions: permissions,
                roleName: this.role.roleName,
                id: this.role._id
            }
            this.$http.post('/updateRoleInfo',params)
            .then((res)=>{
                this.$message({
                    message: res.success?res.message+'成功':res.message+'失败',
                    type: res.success?'success':'danger'
                })
            });
            // this.$http.post('/updateRoleInfo',params,(res)=>{
            //     console.log(res);
            //     alert("更新成功");
            //     this.$message({
            //         message: res.message,
            //         type: res.success?'success':'danger'
            //     })
            // })
        }
    },
    watch: {
        role(newValue){
            setTimeout(()=>{
                //根据要编辑的角色中已有的权限,设置默认选中权限
                var defaultPermission = [];
                this.$store.state.allPermissions.forEach((item)=>{
                    if(newValue.permissions.indexOf(item._id)!=-1){
                        defaultPermission.push(item);
                    }
                })
                this.$refs.tree.setCheckedNodes(defaultPermission)
            },0)
        }
    }
}
</script>-->

<style lang="scss" scoped>
	
</style>